/*
 * Copyright (C) 2005-2008 Alfresco Software Limited.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * 
 * As a special exception to the terms and conditions of version 2.0 of the GPL,
 * you may redistribute this Program in connection with Free/Libre and Open
 * Source Software ("FLOSS") applications as described in Alfresco's FLOSS
 * exception. You should have recieved a copy of the text describing the FLOSS
 * exception, and it is also available here:
 * http://www.alfresco.com/legal/licensing"
 */
package org.alfresco.confluence.plugins;

import java.util.Map;

import com.atlassian.renderer.RenderContext;
import com.atlassian.renderer.v2.macro.BaseMacro;
import com.atlassian.renderer.v2.macro.MacroException;
import com.atlassian.renderer.v2.RenderMode;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.util.http.HttpResponse;
import com.atlassian.confluence.util.http.HttpRetrievalService;

import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.spring.container.ContainerManager;

import org.alfresco.confluence.util.EnumAlfrescoServices;
import org.alfresco.confluence.util.ServiceUtils;

/**
 * Generate macro body based on the Alfresco web script service return.
 * 
 * @author Yong Qu
 *
 */
public class AlfrescoWSMacro extends BaseMacro {

	/**
	 * 
	 */
	private PageManager pageManager;
	/**
	 * 
	 */
	private SpaceManager spaceManager;
	/**
	 * 
	 */
	private HttpRetrievalService httpRetrievalService;

	/* (non-Javadoc)
	 * @see com.atlassian.renderer.v2.macro.Macro#isInline()
	 */
	public boolean isInline()
	{
		return false;
	}

	/* (non-Javadoc)
	 * @see com.atlassian.renderer.v2.macro.Macro#hasBody()
	 */
	public boolean hasBody()
	{
		return false;
	}

	/* (non-Javadoc)
	 * @see com.atlassian.renderer.v2.macro.Macro#getBodyRenderMode()
	 */
	public RenderMode getBodyRenderMode()
	{
		return RenderMode.NO_RENDER;
	}

	/* (non-Javadoc)
	 * @see com.atlassian.renderer.v2.macro.Macro#execute(java.util.Map, java.lang.String, com.atlassian.renderer.RenderContext)
	 */
	public String execute(Map params, String body, RenderContext renderContext)
	throws MacroException
	{
		// Initial implementation of invocation of any GET type of alfresco service
		String op = (String) params.get("0");

		SettingsManager settingsManager = (SettingsManager) ContainerManager.getComponent("settingsManager");
		String baseURL = settingsManager.getGlobalSettings().getBaseUrl();

		try
		{
			HttpResponse response;

			StringBuffer alfServiceUrl = new StringBuffer();

			alfServiceUrl.append(baseURL);

			switch (EnumAlfrescoServices.fromValue(op)) {

			case GENERAL_WS:

				alfServiceUrl.append(ServiceUtils.GENERAL_WS_SERVICE_URL);

				if ( params.containsKey("url")) {

					// try to find the url
					String url = (String) params.get("url");
					alfServiceUrl.append("?url=");
					alfServiceUrl.append(url);

				} else {	

					throw new MacroException("Invalid parameters for document service. Either id parameter or path parameter needs to be provided.") ;				
				}

				break;

			default:

				alfServiceUrl.append(ServiceUtils.REPO_INFO_SERVICE_URL);

			}

			// Common parameters

			// Check if the endpoint parameter is provided

			if (params.containsKey("endpoint")) {

				// try to find the repository endpoint
				String endpoint = (String) params.get("endpoint");
				alfServiceUrl.append("&endpoint=");
				alfServiceUrl.append(endpoint);

			}

			// Check if the view parameter is provided

			if (params.containsKey("view")) {

				// try to find the view
				String view = (String) params.get("view");
				alfServiceUrl.append("&view=");
				alfServiceUrl.append(view);

			}

			response = getHttpRetrievalService().get(alfServiceUrl.toString());
			return ServiceUtils.getContent(response.getResponse());
			//return alfServiceUrl.toString();

		}
		catch(IllegalArgumentException e){
			throw new MacroException("Invalid service type. Valid options are "+EnumAlfrescoServices.values());
		}
		catch (Exception e)
		{
			throw new MacroException(e.getMessage());
		}
	}

	/**
	 * @return
	 */
	public PageManager getPageManager() {
		return pageManager;
	}

	/**
	 * @param pageManager
	 */
	public void setPageManager(PageManager pageManager) {
		this.pageManager = pageManager;
	}

	/**
	 * @return
	 */
	public SpaceManager getSpaceManager() {
		return spaceManager;
	}

	/**
	 * @param spaceManager
	 */
	public void setSpaceManager(SpaceManager spaceManager) {
		this.spaceManager = spaceManager;
	}

	/**
	 * @return
	 */
	public HttpRetrievalService getHttpRetrievalService() {
		return httpRetrievalService;
	}

	/**
	 * @param httpRetrievalService
	 */
	public void setHttpRetrievalService(HttpRetrievalService httpRetrievalService) {
		this.httpRetrievalService = httpRetrievalService;
	}

}
